package com.drcoffee.drcoffeemall.mapper;

import com.drcoffee.drcoffeemall.entry.AfterSale;
import com.drcoffee.drcoffeemall.entry.OrderAttachment;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Mapper
@Repository
public interface AfterSaleMapper {


    @Insert("INSERT INTO t_after_sale(code,type,reason,refund_method,refund_amount,question_describe,phone,img_a,img_b,img_c,img_d,img_e,sq_time,order_code,user_account,account_name,deposit,card_number) " +
            "VALUES (#{code},#{type},#{reason},#{refund_method},#{refund_amount},#{question_describe},#{phone},#{img_a},#{img_b},#{img_c},#{img_d},#{img_e},CURRENT_TIMESTAMP,#{order_code},#{user_account},#{account_name},#{deposit},#{card_number})")
    @Options(useGeneratedKeys = true, keyProperty = "code")
    void addAfterSale(AfterSale afterSale);

    @Update("UPDATE t_after_sale SET status = #{status} WHERE code = #{code}")
    void updateAfterSaleStatus(AfterSale afterSale);

    @Select("SELECT * FROM t_after_sale")
    List<AfterSale> allAfterSale();

    void addAfterAfterOrderPartM(String after_code, List<String> partCodes);

    @Select("SELECT CONCAT(o_file_path, folder) as path FROM t_file_path WHERE bsh_one = 'K' AND bsh_two = 1")
    String getAfterBeforePath();

//    @Select("SELECT a.* FROM t_after_sale a LEFT JOIN t_after_m_order_part b ON a.id = b.after_id LEFT JOIN t_attachment c ON b.order_part_code = c.part_code WHERE a.user_account = #{account} ${typeSql} ${statusSql} AND (a.order_code LIKE #{search} OR c.part_name LIKE #{search}) ORDER BY a.sq_time DESC")

    //    @Select("SELECT * FROM t_after_sale " +
//            "WHERE user_account = #{account} ${typeSql} ${statusSql} AND(code IN " +
//            "(SELECT after_code FROM t_after_m_order_part WHERE order_part_code IN " +
//            "( SELECT part_code FROM t_attachment WHERE language = 'CN' AND part_name LIKE #{search} )) OR order_code LIKE #{search})" +
//            "ORDER BY sq_time DESC")
    List<Map<String, Object>> getAfterSaleList(Map<String, Object> paramMap);

    @Select("SELECT b.*,c.part_name, CONCAT(d.o_file_path, d.folder, c.image_url) as file_path " +
            "FROM t_after_m_order_part a " +
            "LEFT JOIN  t_order_attachment b ON a.order_part_code = b.part_code " +
            "LEFT JOIN t_attachment c on b.part_code = c.part_code,t_file_path d " +
            "WHERE a.after_code = #{code} AND b.order_code = #{order_code} AND c.language = 'CN' AND d.bsh_one = 'G' AND d.bsh_two = 1")
    List<OrderAttachment> getAfterParts(String code, String order_code);

    @Select("SELECT order_part_code FROM t_after_m_order_part WHERE after_code in (SELECT code FROM t_after_sale WHERE order_code = #{order_code} AND status_two = 0)")
    List<String> getNotYetAfterSaleParts(String order_code);

    @Select("SELECT COUNT(0) FROM t_pay_voucher WHERE order_code = #{order_code}")
    Integer getPayVoucherCount(String order_code);

    @Update("UPDATE t_after_sale SET transport_code = #{transporCode},status = 3 WHERE code = #{code} AND status < 4")
    void setTransportByCode(String code, String transporCode);

    @Select("SELECT * FROM t_after_sale WHERE code = #{code}")
    AfterSale getAfterSaleByCode(String code);

    @Update("UPDATE t_after_sale SET status_two = 1,remark = '撤销申请' WHERE code = #{code} AND status < 4")
    void cancelAfterSale(String code);

    @Select("SELECT phone,account_name,deposit,card_number FROM t_after_sale WHERE user_account = #{userAccount} ORDER BY sq_time DESC LIMIT 1")
    AfterSale getLastBankInfo(String userAccount);

    @Update("UPDATE t_after_sale SET status = 6 WHERE code = #{code} AND status = 5")
    void reissueReceiveGoods(String code);

    @Select("SELECT code,sq_time,type,status,reason,status_two,transport_code,transport_code_bf FROM t_after_sale WHERE order_code = #{order_code} ORDER BY sq_time desc")
    List<AfterSale> getAfterSaleListByOrder(String code);
}
